<?php
/**
 * Yotaku shop - yshop
 * Copyright (c) 2016 http://www.yotaku.cn All rights reserved.
 * Author: Creatint <creatint@163.com> <http://creatint.yotaku.cn>
 */
namespace Addons\Attribute;
use Common\Controller\Addon;

class AttributeAddon extends Addon{

    //后台 保存时处理商品数据
    public function admin_attribute_post($post){
//        $post['color'] = 'dd';
        //过滤颜色
        if(empty($post['color'])){
            return $post;
        }
        foreach ($post['color'] as $k=>$v){
            if(empty($v['name'])){
                unset($post['color'][$k]);
            }
        }
        if(empty($post['color'])){
            unset($post['color']);
            return $post;
        }

        //json化颜色
        $post['color'] = json_encode($post['color']);

        //过滤尺码
        if(empty($post['size'])){
            return $post;
        }
        foreach ($post['size'] as $k=>$v){
            if(empty($v['value'])){
                unset($post['size'][$k]);
            }
        }
        if(empty($post['size'])){
            unset($post['size']);
            return $post;
        }
        //json化颜色
        $post['size'] = json_encode($post['size']);

        //过滤规格
        if(empty($post['spec'])){
            return $post;
        }
        foreach ($post['spec'] as $k=>$v){
            if(empty($v['value'])){
                unset($post['spec'][$k]);
            }
        }
        if(empty($post['spec'])){
            unset($post['spec']);
            return $post;
        }
        //json化颜色
        $post['spec'] = json_encode($post['spec']);



        return $post;
    }

    //后台 编辑时显示属性
    public function admin_attribute_edit($info){
        $config = $this->getConfig();
        $attribute_type = $config['attribute_type'];
        $this->assign("attribute_type",$attribute_type);

        //颜色
        $color = $info['color'];
        if(is_string($color)){
            $color = json_decode($color,true);
        }
        if(!empty($color)){
            $this->assign("color",$color);
        }

        //尺寸
        $size = $info['size'];
        if(is_string($size)){
            $size = json_decode($size,true);
        }
        if(!empty($size)){
            $this->assign("size",$size);
        }

        //规格
        $spec = $info['spec'];
        if(is_string($spec)){
            $spec = json_decode($spec,true);
        }
        if(!empty($spec)){
            $this->assign("spec",$spec);
        }

        $this->display("form");
    }

    //前台商品详细页属性显示
    public function goods_attribute($info){

        //全部属性
        $attr = array();

        //颜色
        if(!empty($info['color'])){
            $attr['color'] = json_decode($info['color'],true);
        }
        //尺寸
        if(!empty($info['size'])){
            $attr['size'] = json_decode($info['size'],true);
        }
        //规格
        if(!empty($info['spec'])){
            $attr['spec'] = json_decode($info['spec'],true);
        }

        $this->assign('attri',$attr);
        $this->display("show");
    }

    //前台订单详细页属性显示
    public function order_attribute($attribute){

        //全部属性
        $attr = array();
        if(!empty($attribute) && is_array($attribute)){
            $attr = $attribute;
        }elseif (!empty($attribute) && is_string($attribute)){
            $attr = json_decode($attribute, true);
        }


        $this->assign('attribute',$attr);
        $this->display("order_show");
    }



    public function install(){
        return true;
    }

    public function uninstall(){
        return true;
    }
}